package com.yige.web.controller

import com.mongodb.BasicDBObject
import com.mongodb.DBObject
import com.yige.common.DateUtil
import com.yige.service.mongoService.MongoActionExpenditureService
import com.yige.service.mongoService.MongoActionIncomeService
import com.yige.service.mongoService.MongoExpenditureService
import com.yige.service.mongoService.MongoIncomeService
import com.yige.service.mongoService.MongoManagerService
import com.yige.service.mongoService.MongoMoneyService
import com.yige.service.mongoService.MongoStudentService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.RequestMapping

/**
 * Created by sunhao on 2015/11/10 0010.
 */

@Controller
@RequestMapping("money")
class MoneyController {

    @Autowired
    MongoIncomeService incomeService
    @Autowired
    MongoExpenditureService expenditureService
    @Autowired
    MongoActionExpenditureService actionExpenditureService
    @Autowired
    MongoActionIncomeService actionIncomeService
    @Autowired
    MongoStudentService studentService
    @Autowired
    MongoManagerService managerService
    @Autowired
    MongoMoneyService moneyService

    /*****************控制器模块*****************/

    /**
     * 收入列表
     * @param model
     * @return
     */
    @RequestMapping("list.do")
    def list(Model model){
        List<DBObject> incomeList = incomeService.findListDescendingByDate(),
                expendList = expenditureService.findListDescendingByDate(),
        moneyList = moneyService.findListDescendingByDate()
        def list = new LinkedList()
        def results = incomeTotal(model,incomeList) - expendsTotal(model,expendList)
        incomeList.each {info->
            list.add(buildIncome(info))
        }
        expendList.each {info->
            list.add(buildExpend(info))
        }
        if(moneyList.size() != 0){
            moneyList.each {
                moneyService.deleteOne(it._id)
            }
        }
        list.each {
            def obj = [_id:UUID.randomUUID().toString(),date:it.date,person: it.person,action: it.action,income: it.income,expend: it.expend]as BasicDBObject
            moneyService.insertOne(obj)
        }
        moneyList = moneyService.findListDescendingByDate()
        list = buildMap(moneyList)
        model.addAttribute("results",results)
        model.addAttribute("moneys",list)
        return "money/list"

    }

    /**
     * 多条件联合查询收入列表
     * @param model
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping("find")
    def find(Model model,String startDate, String endDate){
        def query = new BasicDBObject()
        if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
            query.append("date",[$gte:DateUtil.parseDate(startDate), $lte:DateUtil.parseDate(endDate)]as BasicDBObject)
        }
        def moneyList
        if(query == null){
            moneyList = moneyService.findListDescendingByDate()
        }else {
            moneyList = moneyService.findListByQuery(query)
        }
        def income = 0, expend = 0
        moneyList.each {
            if(it.income == 0){
                expend = expend + (-1) * it.expend
            }else {
                income = income + it.income
            }
        }
        def list = buildMap(moneyList)
        model.addAttribute("incomes",income)
        model.addAttribute("expends",expend)
        model.addAttribute("results",income - expend)
        model.addAttribute("moneys",list)
        return "money/list"
    }



    /*****************方法模块*****************/
    def buildIncome(DBObject info){
        def student = studentService.findOneById(info.student)
        def action = actionIncomeService.findOneById(info.action)
        def income = [date:info.date,person:student.name,action:action.name,income:info.total,expend:0]as BasicDBObject
        return income
    }

    def buildExpend(DBObject info){
        def manager = managerService.findOneById(info.manager)
        def expend = [date:info.date,person:manager.name,action:info.action,income:0,expend:(-1)*info.pay]as BasicDBObject
        return expend
    }

    def buildMap(List<DBObject> moneyList){
        def map = new HashMap()
        def list = new LinkedList()
        moneyList.each {info->
                map.put("date",DateUtil.formatDate(info.date))
                map.put("person",info.person)
                map.put("action",info.action)
                map.put("income",info.income)
                map.put("expend",info.expend)
                list.add(map)
                map = new HashMap()
        }
        return list
    }

    def incomeTotal(Model model, List<DBObject> list){
        def total = 0
        list.each {
            total = total + it.total
        }
        model.addAttribute("incomes",total)
        return total
    }

    def expendsTotal(Model model, List<DBObject> list){
        def total = 0
        list.each {
            total = total + it.pay
        }
        model.addAttribute("expends",total)
        return total
    }


}
